#include <stdio.h> 
#include <signal.h> 
#include <sys/time.h> 
    

int limit = 10; 
/* signal process */ 
void timeout_info(int signo) 
{ 
	if(limit == 0) 
	{ 
		printf("Sorry, time limit reached.\n"); 
		return; 
	} 
	printf("only %d senconds left.\n", limit--); 
}

/* init sigaction */ 
void init_sigaction(void) 
{ 
	struct sigaction act; 

	act.sa_handler = timeout_info; 
	act.sa_flags   = 0; 
	sigemptyset(&act.sa_mask); 
	sigaction(SIGPROF, &act, NULL); 
} 

/* init */ 
void init_time(void) 
{ 
	struct itimerval val; 

	val.it_value.tv_sec = 1; 
	val.it_value.tv_usec = 0; 
	val.it_interval = val.it_value; 
	setitimer(ITIMER_PROF, &val, NULL); 
} 

/*
linux下取消用setitimer设置的定时器
setitimer设置it_interval和it_value为零： 
*/
void uninit_time()  
{  
    struct itimerval value;  
    value.it_value.tv_sec = 0;  
    value.it_value.tv_usec = 0;  
    value.it_interval = value.it_value;  
    setitimer(ITIMER_REAL, &value, NULL);  
}

int main(void) 
{ 
	init_sigaction(); 
	init_time(); 
	printf("You have only 10 seconds for thinking.\n"); 

	while(1); 
		return 0; 
} 
